﻿using System;
using System.Windows;
using System.Windows.Controls;
using esquireclockscreensaver.Utils;

namespace esquireclockscreensaver.Behaviors
{
    public sealed class MediaElementUpdatePositionBehavior
    {
        public static readonly DependencyProperty PositionProperty = DependencyProperty.RegisterAttached("Position", typeof(TimeSpan), typeof(MediaElementUpdatePositionBehavior),
                                                                       new PropertyMetadata(TimeSpan.MinValue, OnPositionChanged));

        public static bool GetPosition(DependencyObject obj)
        {
            return (bool)obj.GetValue(PositionProperty);
        }

        public static void SetPosition(DependencyObject obj, bool value)
        {
            obj.SetValue(PositionProperty, value);
        }

        private static void OnPositionChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            var posion = (TimeSpan) args.NewValue;

            (dependencyObject as MediaElement).Do(mediaElement => mediaElement.Position = posion);
        }
    }
}
